package com.bowen.seckill.controller;

import com.bowen.common.utils.R;
import com.bowen.seckill.service.SeckillService;
import com.bowen.seckill.to.SeckillSkuRedisTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class SerckillController {


    @Autowired
    SeckillService seckillService;

    /**
     * 返回当前时间可以参与秒杀的商品信息
     * @return
     */
    @ResponseBody
    @GetMapping("/currentSeckillSkus")
    public R getCurrentSeckillSkus(){
        List<SeckillSkuRedisTo> vos = seckillService.getCurrentSeckillSkus();

        return R.ok().setData(vos);
    }


    @ResponseBody
    @GetMapping("/sku/seckill/{skuId}")
    public R getSkuSeckillInfo(@PathVariable("skuId") Long skuId){

        SeckillSkuRedisTo to = seckillService.getSkuSeckillInfo(skuId);
        return R.ok().setData(to);
    }


    @GetMapping("/kill")
     public String seckill(@RequestParam("killId") String killId, @RequestParam("key") String key,
                           @RequestParam("num") Integer num, Model model){

        //1.判断是否登录
       String ordersn = seckillService.kill(killId,key,num);

       model.addAttribute("orderSn",ordersn);
       return "success";
    }
}
